package com.itboye.eweilisten.me.login;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.base.BaseActivity;
import com.itboye.eweilisten.common.MyEditText;
import com.itboye.eweilisten.me.personal.activity.WebActivity;
import com.itboye.eweilisten.presenter.UserPresenter;
import com.itboye.eweilisten.util.TimeCount;
import com.itboye.eweilisten.util.loadingutil.MAlert;
import com.itboye.eweilisten.util.popwindow.UIAlertView;
import com.itboye.eweilisten.util.sputil.Const;
import com.itboye.eweilisten.util.widget.EmailAutoCompleteTextView;
import com.itboye.eweilisten.util.widget.EmailUtils;
import com.itboye.eweilisten.util.widget.ReigDialogs;
import com.itboye.eweilisten.volley.ResultEntity;

import java.util.Observable;
import java.util.Observer;
import java.util.regex.Pattern;

import static android.R.attr.type;
import static com.itboye.eweilisten.me.zhanghu.UpdateEmailActivity.REGEX_EMAIL;

/**
 * 注册
 */
public class RegisterActivity extends BaseActivity implements Observer, UIAlertView.ClickListenerInterface {
    TextView tvTitle;
    ImageView back;
    UserPresenter userPresenter;

    private TimeCount time;// 倒计时
    LinearLayout li_input_phone;//第一种布局
    LinearLayout li_input_yanzhengma;//第二种布局
    LinearLayout li_input_password;//第三种布局
    TextView one, two, three, tvTest;
    Button refister_ok, btn_commit_yzm, btn_register;
    EmailAutoCompleteTextView refister_youxiang;//邮箱
    MyEditText refister_yzm;//手机号
    EditText ed_yzm;//验证码
    MyEditText ed_pass_one, ed_pass_two;//密码
    private String getYanZheng;
    private String getPhone;
    private CheckBox checkboxRegi;
    private String mExitTime;
    private TextView tvXiyie;
    TextView ed_fasongzhi;
    public static final String telRegex = "[1][34578]\\d{9}";

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_register);
        tvTitle.setText("注册");
        userPresenter = new UserPresenter(this);
        /***
         * 判断是否选中协议，如果没有选中则不能点击
         */
        checkboxRegi.setEnabled(true);
        checkboxRegi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked == true) {
                    Log.d("isCheced", isChecked + "");
                    refister_ok.setBackgroundResource(R.drawable.mybutton_view);
                    refister_ok.setEnabled(true);

                } else {
                    refister_ok.setBackgroundResource(R.drawable.shape_blue02);
                    refister_ok.setEnabled(false);
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.tvTest:
            case R.id.refister_ok://获取验证码

                if (refister_youxiang.getText().toString().equals("")) {
                    MAlert.alert("请输入邮箱地址");
                    return;
                }
                if (!(Pattern.matches(REGEX_EMAIL, refister_youxiang.getText().toString()))) {
                    MAlert.alert("请输入正确的邮箱地址");
                    return;
                }
                if (refister_yzm.getText().toString().equals("")) {
                    MAlert.alert("请输入手机号");
                    return;
                }
                if(!(Pattern.matches(telRegex,refister_yzm.getText().toString()))){
                    MAlert.alert("请输入正确的手机号");
                    return;
                }
                showProgressDialog("发送中..", true);
                getPhone = refister_yzm.getText().toString();
                getYanZheng = refister_youxiang.getText().toString();
                userPresenter.onSecurCode(refister_youxiang.getText().toString(), "1");

                mExitTime = "one";
                break;
            case R.id.btn_commit_yzm://验证验证码
                if (ed_yzm.getText().toString().equals("") || ed_yzm.getText().toString() == null) {
                    MAlert.alert("验证码不能为空");
                    return;
                } else {
                    mExitTime = "two";
                    userPresenter.onYanZhengMa(getYanZheng, ed_yzm.getText().toString(), "1","","");
                }
                break;

            case R.id.btn_register://注册
                if (!ed_pass_one.getText().toString().equals(ed_pass_two.getText().toString())) {
                    MAlert.alert("两次密码不一致");
                    Log.d("paewpapwx", ed_pass_one.getText().toString() + "\n" + ed_pass_two.getText().toString());
                    ed_pass_two.setText("");
                    ed_pass_one.setText("");
                    return;
                }
                if (ed_pass_one.getText().equals("")) {
                    MAlert.alert("密码不能为空");
                    return;
                }
                if (ed_pass_one.getText().toString().length() != 6) {
                    MAlert.alert("请输入六位数字密码");
                    return;
                }
                mExitTime = "three";
                Log.d("getPhone", getPhone);
                userPresenter.onRefister(ed_yzm.getText().toString(), getPhone, refister_youxiang.getText().toString(), ed_pass_one.getText().toString());
                break;
            case R.id.tvXiyie:
                Intent intent = new Intent(this, WebActivity.class);
                intent.putExtra("title", "用户协议");
                intent.putExtra("url", Const.WEB + "web/agreement");
                startActivity(intent);
                break;
            case R.id.back:
                finish();
                break;
        }
    }

    private void setCurrentView(int type) {
        Log.d("return", type + "");
        switch (type) {
            case 1:
                li_input_phone.setVisibility(View.GONE);
                li_input_yanzhengma.setVisibility(View.VISIBLE);
                li_input_password.setVisibility(View.GONE);
                one.setTextColor(getResources().getColor(R.color.gray));
                two.setTextColor(getResources().getColor(R.color.blue_rigister));
                three.setTextColor(getResources().getColor(R.color.gray));
                tvTest.setTextColor(getResources().getColor(R.color.white));
                time = new TimeCount(60000, 1000, tvTest);// 构造CountDownTimer对象
                time.start();
                break;
            case 2:
                tvTest.setVisibility(View.GONE);
                time.start();
                li_input_phone.setVisibility(View.GONE);
                li_input_yanzhengma.setVisibility(View.GONE);
                li_input_password.setVisibility(View.VISIBLE);
                one.setTextColor(getResources().getColor(R.color.gray));
                two.setTextColor(getResources().getColor(R.color.gray));
                three.setTextColor(getResources().getColor(R.color.blue_rigister));
                break;
            case 3:
                finish();
                break;
        }
    }

    private void getYzm(String phone) {
//        userPresenter.sendVerificationCode("+86", phone, 1 + "");
    }


    ReigDialogs dialogs;
    private UIAlertView uiAlertView;

    @Override
    public void update(Observable observable, Object data) {
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
        ResultEntity resultEntity = handlerError(data);
        if (resultEntity != null) {
            //发送验证码
            if (resultEntity.getEventType() == UserPresenter.send_code_success) {
                MAlert.alert(resultEntity.getData(), Gravity.TOP);
                setCurrentView(1);
                ed_fasongzhi.setText("验证码发送至" + refister_youxiang.getText().toString());
            } else if (resultEntity.getEventType() == UserPresenter.send_code_fail) {
                MAlert.alert(resultEntity.getData(), Gravity.TOP);
            } else
                //验证验证码
            if (resultEntity.getEventType() == UserPresenter.yanzheng_code_success) {
                MAlert.alert(resultEntity.getData(), Gravity.TOP);
                setCurrentView(2);
//                if (dialogs == null)
//                    dialogs = new ReigDialogs();
//                dialogs.show(getSupportFragmentManager(), "");
            } else if (resultEntity.getEventType() == UserPresenter.yanzheng_code_fail) {
                MAlert.alert(resultEntity.getData(), Gravity.TOP);
            } else
                //注册
            if (resultEntity.getEventType() == UserPresenter.register_success) {
                setCurrentView(3);
                MAlert.alert(resultEntity.getData(), Gravity.TOP);
                uiAlertView = new UIAlertView(this, "温馨提示！", "注册成功", "好", "", type);
                uiAlertView.show();
                uiAlertView.setClicklistener(this);

            } else if (resultEntity.getEventType() == UserPresenter.register_fail) {
                MAlert.alert(resultEntity.getData(), Gravity.TOP);
//                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
//                intent.putExtra("eimal", refister_youxiang.getText().toString());
//                startActivityForResult(intent, 101);
//                finish();
            }
        }

    }

    @Override
    public void doLeft() {
        uiAlertView.dismiss();
        finish();
    }

    @Override
    public void doRight() {

    }
}

